/*
 * Created on 17.09.2006 by leck
 *
 */
package de.devisnik.eidle.flashlight;

import java.util.Random;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;

public class MultiShapeSpot implements ISpot {

	private final int itsSpotCount;
	private final Spot[] itsSpots;
	private Region itsRegion;
	private final int itsRadius;
	private final IShapeFactory itsShapeFactory;
	private final Random random = new Random();

	public MultiShapeSpot(final int spots, final int radius,
			final IShapeFactory shapeFactory) {
		itsSpotCount = spots;
		itsRadius = radius;
		itsShapeFactory = shapeFactory;
		itsSpots = createRandomSpots(itsSpotCount);
	}

	private Spot[] createRandomSpots(final int number) {
		final Spot[] spotArray = new Spot[number];
		for (int i = 0; i < number; i++) {
			spotArray[i] = createRandomSpot();
		}
		return spotArray;
	}

	private Spot createRandomSpot() {
		final int radius = itsRadius;
		final Point center = createRandomPoint(new Rectangle(radius, radius,
				500, 500));
		final int dirX = createRandomDirection();
		final int dirY = createRandomDirection();
		final int speedX = createRandomSpeed();
		final int speedY = createRandomSpeed();

		return new Spot(itsRadius, center, dirX, dirY, speedX, speedY,
				itsShapeFactory);
	}

	private int createRandomSpeed() {
		return random.nextInt(15) + 1;
	}

	private int createRandomDirection() {
		if (random.nextBoolean()) {
			return 1;
		}
		return -1;
	}

	private Point createRandomPoint(final Rectangle rectangle) {
		final int randomX = random.nextInt(rectangle.width);
		final int randomY = random.nextInt(rectangle.height);
		return new Point(randomX + rectangle.x, randomY + rectangle.y);
	}

	public void dispose() {
		for (int i = 0; i < itsSpots.length; i++) {
			itsSpots[i].dispose();
		}
		if (itsRegion != null) {
			itsRegion.dispose();
		}
	}

	public Region getRegion() {
		if (itsRegion != null) {
			itsRegion.dispose();
		}
		itsRegion = new Region();
		for (int i = 0; i < itsSpots.length; i++) {
			itsRegion.add(itsSpots[i].getRegion());
		}
		return itsRegion;
	}

	public Rectangle move(final Rectangle rectangle) {
		final Rectangle[] boxes = new Rectangle[itsSpots.length];
		for (int i = 0; i < itsSpots.length; i++) {
			boxes[i] = itsSpots[i].move(rectangle);
		}
		for (int i = 1; i < itsSpots.length; i++) {
			boxes[0] = boxes[0].union(boxes[i]);
		}
		return boxes[0];
	}

	public void setRadius(final int radius) {
		for (int i = 0; i < itsSpots.length; i++) {
			itsSpots[i].setRadius(radius);
		}
	}

	public void turnLeft() {
		for (int i = 0; i < itsSpots.length; i++) {
			final Point direction = itsSpots[i].getDirection();
			itsSpots[i].setDirection(direction.y, -direction.x);
			final Point speed = itsSpots[i].getSpeed();
			itsSpots[i].setSpeed(speed.y, speed.x);
		}
	}

	public void turnRight() {
		for (int i = 0; i < itsSpots.length; i++) {
			final Point direction = itsSpots[i].getDirection();
			itsSpots[i].setDirection(-direction.y, direction.x);
			final Point speed = itsSpots[i].getSpeed();
			itsSpots[i].setSpeed(speed.y, speed.x);
		}
	}
}